<html>
<head><title>CS143 Movie Data Query (Project 1B)</title></head>
<body>

<h1>Movie Database Query</h1>

by Samuel Jun<br />
SID: 003-955-212<br />

<p>To access the movie database, please use the input box below to type in a valid MySQL query statement. Thank You.</p>

<form method = "GET">
<textarea name="query" cols="80" rows="10"></textarea>
<input type="submit" value="Submit" />
</form>

<?php
//Start of the php section of the code
$input = $_GET["query"];

$db_connection = mysql_connect("localhost", "cs143", "");
//Error message if the client could not connect to the database
if (!db_connection)
{
    $errmsg = mysql_error($db_connection);
    print "<b>Connection failed:</b> $errmsg<br />";
    exit(1);
}

mysql_select_db("CS143", $db_connection);
if (isset($input))
{
    $result = mysql_query($input, $db_connection);
    //Error Message if the query doesn't work
    if (!$result)
    {
        $errmsg = mysql_error($db_connection);
        print "<b>Query failed:</b> $errmsg<br />";
        exit(1);
    }
    else
    {
        print "<p>Below is the result of the query:</p>\n";
        //Create a table of the query
        print "<table border=1 cellspacing=1 cellpadding=2>\n";
        print "<tr align=center>";
        //This section is for the title of columns of the result
        $i = 0;
        while($i != mysql_num_fields($result))
        {
            $col = mysql_field_name($result, $i);
            print "<td><b>$col</b></td>";
            $i++;
        }
        print "</tr>\n";
        //This section is for the actually tuples of the results
        while($row = mysql_fetch_row($result))
        {
            print "<tr align=center>";
            foreach ($row as $value)
            {
                if (is_null($value))
                {
                    print "<td>N/A</td>";
                }
                else
                    print "<td>$value</td>";
            }
            print "</tr>\n";
        }
        print "</table>\n";
    }
}

mysql_close($db_connection);
?>

</body>
</html>
